#ifndef MERGE_H
#define MERGE_H

#include "drm_drv/drm_plane_drv.h"
#include "v4l2_drv/v4l2_capture.h"
#include <unistd.h>

enum Pipe_Merge_State{
    pm_init,
    pm_runing,
    pm_stoping,
    pm_stoped
};

enum Pipe_MERGEMODE
{
    Pipe_HONGWAI = 0,
    Pipe_WEIGUANG,
    Pipe_MERGE,
};

struct Pipe_MixPositionItem {
    unsigned int  x_hzOffset_hw; //红外X轴平移
    unsigned int  y_verOffset_hw; //红外Y轴平移
    unsigned int  x_scale_hw;    //红外X轴缩放
    unsigned int  y_scale_hw;    //红外Y轴缩放
};
struct Pipe_CameraBriCon {
    unsigned int  fcBrightness;
    unsigned int  fcContrast;
    unsigned int  bcBrightness;
    unsigned int  bcContrast;
    unsigned int  lcBrightness;
    unsigned int  lcContrast;
    unsigned int  rcBrightness;
    unsigned int  rcContrast;
};


void format_convert(char *databuff, int datalen);
void Flip_image(unsigned char *srcbuff, unsigned char *dstbuff, int datalen);

extern Pipe_CameraBriCon g_cameraBriCon;
void setCameraBrightness(Pipe_CameraBriCon cameraBrightnessContrast);
void setCameraContrast(Pipe_CameraBriCon cameraBrightnessContrast);

class Pipe_Merge {

private:
    struct drm_plane *plane=NULL;
    Capture *cap0=NULL;
    Capture *cap1=NULL;
     Pipe_Merge_State status=pm_init;
     std::mutex status_mutex;
     std::mutex mode_mutex;
     std::thread *pipeThread;

     void merge_thread_run();
     void merge_inf_thread_run();
     void merge_wg_thread_run();

public:

        Pipe_MERGEMODE g_mergeMode =Pipe_MERGE;
        bool gRedColorSwitch = true;
        Pipe_MixPositionItem g_mixPosition;
//        bool flipSwitch[4] = {false, true, true, true};


        Pipe_Merge_State get_status(void){
            Pipe_Merge_State tmp;
            status_mutex.lock();
            tmp=status;
            status_mutex.unlock();
            return tmp;
        }
        void start(Capture *cap0,Capture *cap1,struct drm_plane *plane);
        void start_alone(Capture *cap0,Capture *cap1,struct drm_plane *plane, bool is_inf);
        void re_run(){
            status_mutex.lock();
            if(status==pm_stoped){
                status=pm_runing;
            }
            status_mutex.unlock();
        }
        void set_status(Pipe_Merge_State tmp){
            status_mutex.lock();
            status=tmp;
            status_mutex.unlock();
        }
        void wait_stoping(void){
            while(get_status()!=pm_stoped){
                usleep(100);
            }
        }
        void stop(){
            if(get_status()==pm_runing){
                set_status(pm_stoping);
                wait_stoping();
            }
        }

        void setMergemode(Pipe_MERGEMODE mode);
        Pipe_MERGEMODE getMergemode();
        void setColormode(bool switch_flag);
        bool getColormode();
        void setMergeposition(Pipe_MixPositionItem position);
        Pipe_MixPositionItem getMergeposition();
        int get_vmin_value();

    };

#endif // MERGE_H

